package order

import (
	"fmt"
	"strconv"

	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/response"
	"gitcode.com/G-YT/iHealth25/server/model/order"
	orderReq "gitcode.com/G-YT/iHealth25/server/model/order/request"
	"gitcode.com/G-YT/iHealth25/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type OrderApi struct{}

// DeleteOrder 删除订单
// @Tags Order
// @Summary 删除订单
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body order.Order true "删除订单"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /order/deleteOrder [delete]
func (o *OrderApi) DeleteOrder(c *gin.Context) {
	var order order.Order
	err := c.ShouldBindJSON(&order)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = orderService.DeleteOrder(order.ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// BatchDeleteOrder 批量删除订单
// @Tags Order
// @Summary 批量删除订单
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body orderReq.BatchDeleteOrderRequest true "批量删除订单"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /order/batchDeleteOrder [delete]
func (o *OrderApi) BatchDeleteOrder(c *gin.Context) {
	var req orderReq.BatchDeleteOrderRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := orderService.BatchDeleteOrder(req); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// FindOrder 用id查询订单
// @Tags Order
// @Summary 用id查询订单
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询订单"
// @Success 200 {object} response.Response{data=order.Order,msg=string} "查询成功"
// @Router /order/findOrder [get]
func (o *OrderApi) FindOrder(c *gin.Context) {
	IDStr := c.Query("ID")
	ID, err := strconv.ParseUint(IDStr, 10, 32)
	if err != nil {
		response.FailWithMessage("ID格式错误", c)
		return
	}
	reOrder, err := orderService.GetOrder(uint(ID))
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(reOrder, c)
}

// GetOrderList 分页获取订单列表
// @Tags Order
// @Summary 分页获取订单列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query orderReq.OrderSearch true "分页获取订单列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /order/getOrderList [get]
func (o *OrderApi) GetOrderList(c *gin.Context) {
	var pageInfo orderReq.OrderSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := orderService.GetOrderInfoList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetUserOrderList 获取用户订单列表
// @Tags Order
// @Summary 获取用户订单列表
// @Accept application/json
// @Produce application/json
// @Param data query orderReq.UserOrderListRequest true "获取用户订单列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /order/getUserOrderList [get]
func (o *OrderApi) GetUserOrderList(c *gin.Context) {
	var pageInfo orderReq.UserOrderListRequest
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 从JWT中获取用户ID
	claims, _ := utils.GetClaims(c)
	userID := claims.BaseClaims.ID

	list, total, err := orderService.GetUserOrderList(userID, pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// CreatePayOrder 创建支付订单（无鉴权）
// @Tags Order
// @Summary 创建支付订单
// @Accept application/json
// @Produce application/json
// @Param data body orderReq.CreatePayOrderRequest true "创建支付订单"
// @Success 200 {object} response.Response{data=orderRes.CreatePayOrderResponse,msg=string} "创建成功"
// @Router /order/createPayOrder [post]
func (o *OrderApi) CreatePayOrder(c *gin.Context) {
	var req orderReq.CreatePayOrderRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)
	fmt.Printf("userID: %d", userID)
	orderInfo, InfoErr := orderService.CreatePayOrderWithSpecs(userID, req.ProductId, req.Quantity, req.SelectedSpecs)
	if InfoErr != nil {
		global.GVA_LOG.Error("创建支付订单失败!", zap.Error(InfoErr))
		response.FailWithMessage(InfoErr.Error(), c)
		return
	}
	response.OkWithDetailed(orderInfo, "订单创建成功", c)
}
